/**
 * @description 是否为空
 * @param {unknown} value
 * @return {Boolean}
 */
export const isEmpty = (value: unknown) => {
    return value == null && typeof value == "undefined";
};

/**
 * 判断是否是url
 * @param str
 * @returns
 */
export function isUrl(str: string): boolean {
    return /^(https?:)?\/\//.test(str);
}

/**
 * 获取当前域名
 * @returns 
 */
export function getDomain() {
	const { protocol, hostname, port } = window.location;
	return `${protocol}//${hostname}${port ? `:${port}` : ""}`;
}

/**
 * 获取路径
 * @param path
 * @returns
 */
export function getUrl(path: string): string {
    return isUrl(path)
        ? path
        : `${useRuntimeConfig().public.VITE_APP_BASE_URL}/${path}`;
}

/**
 * 获取路由path
 * @param routePath
 * @param symbol
 * @returns
 */
export function getRoutePath(routePath:any, symbol = "."): string {
    let path = routePath == "/" ? "/index" : routePath;
    path = !path.lastIndexOf("/") ? `${path}/index` : path;
    return path.replace("/", "").replaceAll("/", symbol);
}

/**
 * 获取页面标题（兼容插件）
 * @param route 当前路由对象
 * @returns 
 */
export function getPageTitle(route?: any): string {
    const addonKey = route?.meta?.addon;
    let routePath  = '';
    if(route?.matched?.length > 0) {
        routePath = getRoutePath(route.matched[route.matched.length - 1].path);
    } else {
        routePath = getRoutePath(route.path);
    }
    if (addonKey) {
        return routePath.replace('addons.' + addonKey, 'addon_' + addonKey);
    }
    
    return 'pages.' + routePath;
}
